package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.dao.BspsDevBindInfoMapper;
import cc.rengu.igas.bsps.common.entity.DevBindInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/04/02
 * Updated by zhangxuran on 2020/04/10  selectBspsDevBindInfoByDevUniqueIdAndDevType方法 增加默认查询条件绑定状态为绑定
 */
public class BspsDevBindInfoMapperImpl implements BspsDevBindInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public int updateBspsDevBindInfoStatusByUserIdAndMchntNo(String instId, DevBindInfo devBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsDevBindInfoStatusByUserIdAndMchntNo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, devBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("解绑收银员绑定设备失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public List<DevBindInfo> selectBspsDevBindInfoListByUserIdAndDevType(String instId, String bindUserId, String mchntNo, String devType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsDevBindInfoListByUserIdAndDevType");
        DevBindInfo devBindInfo = new DevBindInfo();
        devBindInfo.setInstId(instId);
        devBindInfo.setBindUserId(bindUserId);
        devBindInfo.setBindMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, devBindInfo, DevBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询语音设备信息失败,instId:<{}>,bindUserId:<{}>,mchntNo:<{}>,devType:<{}>", instId, bindUserId, mchntNo, devType);
            return null;
        }
        return objectList.stream().map(item -> (DevBindInfo) item).collect(Collectors.toList());
    }

    @Override
    public int insertBspsDevBindInfo(DevBindInfo devBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspsDevBindInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, devBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记设备绑定信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", devBindInfo.getInstId(), devBindInfo.getBindMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public DevBindInfo selectBspsDevBindInfoByDevUniqueIdAndDevType(String instId, String devUniqueId, String devType, String bindStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsDevBindInfoByDevUniqueIdAndDevType");
        DevBindInfo devBindInfo = new DevBindInfo();
        devBindInfo.setInstId(instId);
        devBindInfo.setBindDevId(devUniqueId);
        devBindInfo.setBindDevType(devType);
        if (!StringUtil.isEmptyOrNull(bindStatus)) {
            devBindInfo.setBindStatus(bindStatus);
        }
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, devBindInfo, devBindInfo);
        if (0 != iReturnCode) {
            rglog.error("获取设备绑定信息失败,instId:<{}>,devUniqueId:<{}>,devType:<{}>,iReturnCode:<{}>", instId, devUniqueId, devType, iReturnCode);
            return null;
        }
        return devBindInfo;
    }

    @Override
    public int updateBspsDevBindInfoStatusByPrimaryKey(DevBindInfo devBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsDevBindInfoStatusByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, devBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("解绑绑定设备失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public List<DevBindInfo> selectBspsDevBindInfoByModelAndProduct(String instId, String devBrand, String devModel) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsDevBindInfoByModelAndProduct");
        DevBindInfo devBindInfo = new DevBindInfo();
        devBindInfo.setInstId(instId);
        devBindInfo.setBindDevBrand(devBrand);
        devBindInfo.setBindDevModel(devModel);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, devBindInfo, DevBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询语音设备信息失败,instId:<{}>,devBrand:<{}>,devModel:<{}>", instId, devBrand, devModel);
            return null;
        }
        return objectList.stream().map(item -> (DevBindInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateVoiceDevInfo(DevBindInfo devBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateVoiceDevInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, devBindInfo, null);
        if (0 != iReturnCode && BspsAppParamConstant.NODATA_CODE != iReturnCode) {
            rglog.error("绑定语音设备失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsDevBindInfoByPrimaryKey(DevBindInfo devBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsDevBindInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, devBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新设备绑定用户失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;

    }

}
